#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 导入相应的库文件
import urllib.request
import requests
import os
import re
import openpyxl
import mysql
import mysql.connector
from threading import Thread

# base_dir="E:\data\小说\\"
# 初始化列表，存入爬虫数据
dict = [('0', '其他小说'), ('1', '玄幻奇幻'), ('2', '修真仙侠'), ('3', '都市青春'), ('4', '军事历史'),
        ('5', '网游竞技'), ('6', '科幻灵异'), ('7', '言情穿越'), ('8', '耽美同人'), ('9', '台言古言'),
        ('10', '武侠小说'), ('11', '古典文学'), ('12', '侦探推理'), ('13', '外国文学'), ('14', '当代现代'),
        ('15', '童话故事'), ('16', '传记纪实'), ('17', '韩流青春'), ('18', '影视文学')]


def geturl_into_excel(i):
    mydb = mysql.connector.connect(
        host="165.135.111.114",  # 数据库主机地址
        port='31004',
        user="root",  # 数据库用户名
        database="test",  # 数据库名称 没有的话插入操作报错
        passwd="211314wccA"  # 数据库密码

    )
    mycursor = mydb.cursor()
    row = 1
    for j in range(99999):
        # if j < 132:
        #     continue
        html = requests.get("https://ixdzs8.com/sort/" + i.__str__() + "/index-0-0-0-" + (j + 1).__str__() + ".html")
        html.encoding = 'utf-8'
        # print(html.text)
        labelfind = '<h3 class="bname"><a href="/read/(.*?)/"  title="(.*?)">'
        labels = re.findall(labelfind, html.text, re.S)
        print(
            "===========" + dict[i][1].__str__() + "--> 第" + (j + 1).__str__() + "页================================")
        print(labels)
        if len(labels) == 0:
            print("数组为空")
            break
        for (key, value) in labels:
            book_type_insert_sql = "INSERT INTO fiction_table (f_code, f_name,type_id,type_name) VALUES ('" + key.__str__() + "', '" + value.__str__() + "', '" + i.__str__() + "', '" + \
                                   dict[i][1].__str__() + "') "
            mycursor.execute(book_type_insert_sql)
            mydb.commit()
            print("==================第" + (
                row).__str__() + "行" + value.__str__() + "================================")
            row += 1


# f.save(dict[i][1]+".xlsx")  # 保存文件

def get_url_and_author(url):
    html = requests.get(url)
    html.encoding = 'utf-8'
    print("==================================================")
    print(html.text)
    print("==================================================")
    labelfind = '<title>(.*?)</title>'
    labels = re.findall(labelfind, html.text, re.S)
    print(labels[0])
    labelfindurl = '<span class="btn-solid"><a href="(.*?)">TXT下载</a></span>'
    labelurls = re.findall(labelfindurl, html.text, re.S)
    print(labelurls[0])


def test(url):
    html = requests.get(url)
    html.encoding = 'utf-8'
    print("==================================================")
    print(html.text)
    print("==================================================")
    labelfind = '<h3 class="bname"><a href="/read/(.*?)/"  title="(.*?)">'
    labels = re.findall(labelfind, html.text, re.S)
    print("==================================================")
    print(labels)
    labelfind = '<title>(.*?)</title>'
    labels = re.findall(labelfind, html.text, re.S)
    print("==================================================")
    print(labels)


def get_file(id, type, name):
    url = "https://down7.ixdzs8.com/" + str(id) + ".zip"
    print(url)
    try:
        if not os.path.exists('./' + type + '/'):
            os.makedirs('./' + type + '/')
        print('./' + type + '/' + name + ".zip")
        urllib.request.urlretrieve(url, './' + type + '/' + name + ".zip")
    except Exception as e:
        print("===捕获异常" + e.__str__())
        get_file(id, type, name)


def start_download():
    current_id = 2
    text_id = 1
    for i in range(10):
        workbook = openpyxl.load_workbook(dict[i][1] + '.xlsx')
        sheet = workbook["Sheet0"]
        count = 0
        for one_column_data in sheet.iter_rows():
            if text_id > current_id:
                if count == 0:
                    count += 1
                    print('=====' + dict[i][1] + '=====')
                else:
                    print('===开始下载==' + str(one_column_data[0].value) + ': ' + str(one_column_data[1].value))
                    get_file(str(one_column_data[0].value), dict[i][1], str(one_column_data[1].value))
                    print(text_id)
            text_id += 1


# 程序主入口
if __name__ == '__main__':
    # test("https://ixdzs8.com/sort/11/")
    thread1 = Thread(target=geturl_into_excel, args=(5,))
    # thread2 = Thread(target=geturl_into_excel, args=(7,))
    # thread3 = Thread(target=geturl_into_excel, args=(8,))
    # thread4 = Thread(target=geturl_into_excel, args=(9,))
    # thread5 = Thread(target=geturl_into_excel, args=(10,))

    thread1.start()
    # thread2.start()
    # thread3.start()
    # thread4.start()
    # thread5.start()
